package com.atguigu.gulimall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.atguigu.gulimall.product.entity.AttrEntity;
import com.atguigu.gulimall.product.service.AttrAttrgroupRelationService;
import com.atguigu.gulimall.product.service.AttrService;
import com.atguigu.gulimall.product.service.CategoryService;
import com.atguigu.gulimall.product.vo.AttrGroupRelationVO;
import com.atguigu.gulimall.product.vo.AttrGroupWhitAttrsVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.atguigu.gulimall.product.entity.AttrGroupEntity;
import com.atguigu.gulimall.product.service.AttrGroupService;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.R;

import javax.annotation.Resource;


/**
 * 属性分组相关
 *
 * @author zhuanning
 * @email zhuanning@gmail.com
 * @date 2020-05-23 00:42:49
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;
    @Resource
    private CategoryService categoryService;
    @Resource
    private AttrService attrService;
    @Resource
    private AttrAttrgroupRelationService relationService;



    /***
     * @Description: 属性分组关联
     * @author zhuanning
     * @date 2022/3/19 4:17 下午
     * @param voList
     * @return
     */
    @PostMapping("/attr/relation")
    public R addRelation(@RequestBody List<AttrGroupRelationVO> voList){
        relationService.saveBatch(voList);
        return R.ok();
    }

    /***
     * @Description: 分组 关联属性列表查询
     * @author zhuanning
     * @date 2022/3/18 11:07 下午
     * @param attrgroupId
     * @return
     */
    @RequestMapping("/{attrgroupId}/attr/relation")
    public R attrRelation(@PathVariable("attrgroupId")Long attrgroupId){
        List<AttrEntity> list = attrService.getAttrRelations(attrgroupId);
        return R.ok().put("data", list);
    }

    /***
     * @Description: 查询分类下所有分组信息
     * @author zhuanning
     * @date 2022/3/19 4:20 下午
     * @param catelogId
     * @return
     */
    @RequestMapping("/{catelogId}/withattr")
    public R getAttrGroupWhithAttrs(@PathVariable("catelogId")Long catelogId){
        //1.查询当前分类下的所有分组
        //2.查询分组下的所有属性返回
        List<AttrGroupWhitAttrsVO> attrGroupWhitAttrsList = attrGroupService.getAttrGroupWhithAttrsByCatelogId(catelogId);
        return R.ok().put("data", attrGroupWhitAttrsList);
    }

    /***
     * @Description: 分页查询当前分组没有关联的所有属性
     * @author zhuanning
     * @date 2022/3/19 10:25 上午
     * @param attrgroupId
     * @return
     */
    @RequestMapping("/{attrgroupId}/noattr/relation")
    public R noAttrRelation(@PathVariable("attrgroupId")Long attrgroupId,@RequestParam Map<String, Object> params){
        PageUtils page = attrService.getNoAttrRelations(params,attrgroupId);
        return R.ok().put("page", page);
    }

    /***
     * @Description: 属性分组分页列表
     * @author zhuanning
     * @date 2022/3/18 6:56 下午
     * @param params
     * @return
     */
    @RequestMapping("/list/{categoryId}")
    public R list(@RequestParam Map<String, Object> params,@PathVariable(value = "categoryId") Long categoryId){
        PageUtils page = attrGroupService.queryPage(params,categoryId);
        return R.ok().put("page", page);
    }

    /***
     * @Description: 查询所有分组属性列表
     * @author zhuanning
     * @date 2022/3/18 7:00 下午
     * @return
     */
    @RequestMapping("/listAll")
    public R listAll(){
        List<AttrGroupEntity> list = attrGroupService.listAll();
        return R.ok().put("data", list);
    }


    /***
     * @Description: 批量删除
     * @author zhuanning
     * @date 2022/3/18 11:07 下午
     * @param relations
     * @return
     */
    @PostMapping("/attr/relation/delete")
    public R deleteRelation(@RequestBody AttrGroupRelationVO[] relations){
        attrService.deleteRelation(relations);
        return R.ok();
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    //@RequiresPermissions("product:attrgroup:info")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        // 根据当前所在分类路径查询到完整路径
        List<Long> catelogPaths = categoryService.findCatelogPath(attrGroup.getCatelogId());
        attrGroup.setCatelogPath(catelogPaths);
        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:attrgroup:save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:attrgroup:update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:attrgroup:delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

}
